<?php

abstract class marcasDAOImpl {

    public static function Select($search) {
        if (DB::$mysqli = DB::Connection()) {
            $query = "select id, url, name, detached from brands";
            if (isset($search)) {
                if (is_int($search)) {
                    $query = $query . " where id=?";
                } else if(is_string($search)) {
                    $query = $query . " where url=?";
                }
            }
            DB::$_stmt = DB::$mysqli->prepare($query);
            if (isset($search)) {
                if (is_int($search)) {
                    DB::$_stmt->bind_param('i', $search);
                } else if(is_string($search)) {
                    DB::$_stmt->bind_param('s', $search);
                }
            }
            if (!DB::$_stmt->execute()) {
                DB::$mysqli->error;
            }
            DB::$_stmt->bind_result($data_id, $data_url, $data_name, $data_detached);
            while (DB::$_stmt->fetch()) {
                $data[] = array('id' => $data_id, 'url' => $data_url, 'name' => $data_name, 'detached' => $data_detached);
            }
            return $data;
        } else {
            error_log("erro ao conectar!");
        }
    }

    public static function Listar($limit = NULL, $page = NULL, $search = NULL) {
        if (DB::$mysqli = DB::Connection()) {
            $query = "select id, url, name, detached from brands";
            if (isset($search)) {
                if (is_numeric($search)) {
                    $query = $query . " where id=?";
                } else {
                    $query = $query . " where name like ?";
                }
            }
            if (isset($limit)) {
                $query = $query . " limit " . $limit;
            }
            DB::$_stmt = DB::$mysqli->prepare($query);
            if (isset($search)) {
                if (is_numeric($search)) {
                    DB::$_stmt->bind_param('i', $search);
                } else {
                    DB::$_stmt->bind_param('s', $search);
                }
            }
            if (!DB::$_stmt->execute()) {
                DB::$mysqli->error;
            }
            DB::$_stmt->bind_result($data_id, $data_url, $data_name, $data_detached);
            while (DB::$_stmt->fetch()) {
                $data[] = array('id' => $data_id, 'url' => $data_url, 'name' => $data_name,'detached'=>$data_detached);
            }
            return $data;
        } else {
            error_log("erro ao conectar!");
        }
    }

    public static function Update($url, $name, $detached, $id) {
        if (DB::$mysqli = DB::Connection()) {
            $query = "update brands set url=?,name=?, detached=?
                where id=?";
            DB::$_stmt = DB::$mysqli->prepare($query);
            DB::$_stmt->bind_param('ssii', $url, $name, $detached, $id);
            DB::$_stmt->execute();
            return DB::$_stmt;
        } else {
            error_log("erro ao conectar!");
        }
    }

    public static function Insert($url, $name, $detached) {
        if (DB::$mysqli = DB::Connection()) {
            $query = "insert into brands
                (url, name,detached) 
                values (?,?,?)";
            DB::$_stmt = DB::$mysqli->prepare($query);
            DB::$_stmt->bind_param('ssi', $url, $name, $detached);
            if (!DB::$_stmt->execute()) {
                echo DB::$mysqli->error;
            }
            return DB::$mysqli->insert_id;
        } else {
            error_log("erro ao conectar!");
        }
    }

    public static function Delete($id) {
        if (DB::$mysqli = DB::Connection()) {
            $query = "delete from brands where id = ?";
            DB::$_stmt = DB::$mysqli->prepare($query);
            DB::$_stmt->bind_param('i', $id);
            DB::$_stmt->execute();
            return DB::$_stmt;
        } else {
            error_log("erro ao conectar!");
        }
    }

}

?>
